home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / NervousText.java < prev    next >
Text File  |  1998-08-27  |  9KB  |  336 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.beans.PropertyVetoException;
  7. import java.beans.PropertyChangeListener;
  8. import java.beans.VetoableChangeListener;
  9. import java.beans.PropertyChangeEvent;
  10.  
  11. //     01/29/97    TWB    Integrated changes from Macintosh
  12. //    05/31/97    RKM    Updated to support Java 1.1
  13. //                    Made properties bound & constrained
  14. //  07/15/97    CAR marked fields transient as needed
  15. //  07/15/97    CAR added preferredSize
  16.  
  17. /**
  18.  * NervousText control.
  19.  * Creates animated text in which each letter moves independently of all other letters.
  20.  *
  21.  * @version 1.0, Nov 26, 1996
  22.  * @author Symantec
  23.  */
  24.  
  25. public class NervousText
  26.     extends Canvas
  27.     implements Runnable
  28. {
  29.     char                separated[];
  30.     String                text;
  31.     boolean             paused;
  32.     transient Thread    jitter = null;
  33.  
  34.     /**
  35.      * Create defalut NervousText object. Default object contains the word "text".
  36.      */
  37.     public NervousText()
  38.     {
  39.         paused = false;
  40.  
  41.         try
  42.         {
  43.             setText("text");
  44.         }
  45.         catch (PropertyVetoException veto) {}
  46.  
  47.     }
  48.  
  49.     //
  50.     // Properties
  51.     //
  52.  
  53.     /**
  54.      * Specify the text that will be displayed by NervousText.
  55.      *
  56.      * @param str text to be shown as nervous text
  57.      *
  58.      * @exception PropertyVetoException
  59.      * if the specified property value is unacceptable
  60.      */
  61.     public void setText(String newText)
  62.         throws PropertyVetoException
  63.     {
  64.         if (!symantec.itools.util.GeneralUtils.objectsEqual(text,newText))
  65.         {
  66.             String oldText = text;
  67.  
  68.             vetos.fireVetoableChange("Text", oldText, newText);
  69.  
  70.             text = newText;
  71.  
  72.             changes.firePropertyChange("Text", oldText, newText);
  73.  
  74.             //Extract String into char[] (handle null String)
  75.             int length = text != null ? text.length() : 0;
  76.             separated = new char[length];
  77.             if (text != null)
  78.                 text.getChars(0, length, separated, 0);
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Obtain the text that is currently being displayed.
  84.      *
  85.      * @return text that is being shown by this component
  86.      */
  87.     public String getText()
  88.     {
  89.         return text;
  90.     }
  91.  
  92.     /**
  93.      * Temporarily suspend the animation of the nervous text. Identical
  94.      * to pause(boolean).
  95.      * @param f pause the animation of true
  96.      *
  97.      * @exception PropertyVetoException
  98.      * if the specified property value is unacceptable
  99.      */
  100.     public void setPaused(boolean newIsPaused)
  101.         throws PropertyVetoException
  102.     {
  103.         if (paused != newIsPaused)
  104.         {
  105.             Boolean oldPausedBool = new Boolean(paused);
  106.             Boolean newPausedBool = new Boolean(newIsPaused);
  107.  
  108.             vetos.fireVetoableChange("Paused", oldPausedBool, newPausedBool);
  109.  
  110.             paused = newIsPaused;
  111.  
  112.             changes.firePropertyChange("Paused", oldPausedBool, newPausedBool);
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Obtain animator's current state.
  118.      *
  119.      * @return true if the animator is paused, false if it is running
  120.      */
  121.     public boolean isPaused()
  122.     {
  123.         return paused;
  124.     }
  125.  
  126.     //
  127.     // Methods
  128.     //
  129.  
  130.     /**
  131.      * @deprecated
  132.      * @see setPaused
  133.      */
  134.     public void pause(boolean f)
  135.     {
  136.         //!!!RKM!!! Silent catch veto and deprecate the method
  137.         try
  138.         {
  139.             setPaused(f);
  140.         }
  141.         catch (PropertyVetoException veto) {}
  142.     }
  143.  
  144.     /**
  145.      * NervousText thread body.  This method is called by the Java virtual
  146.      * machine to when this thread starts.
  147.      */
  148.     public void run()
  149.     {
  150.         while (true)
  151.         {
  152.  
  153.             try
  154.             {
  155.                 Thread.sleep(150);
  156.             }
  157.             catch(Exception e)
  158.             {
  159.             }
  160.             if (!paused)
  161.             {
  162.                 repaint();
  163.             }
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Tells this component that it has been added to a container.
  169.      * This is a standard Java AWT method which gets called by the AWT when
  170.      * this component is added to a container. Typically, it is used to
  171.      * create this component's peer.
  172.      *
  173.      * It has been overridden here to start the nervous text thread.
  174.      *
  175.      * @see #removeNotify
  176.      */
  177.     public void addNotify()
  178.     {
  179.         super.addNotify();
  180.  
  181.         jitter = new Thread(this);
  182.         jitter.start();
  183.     }
  184.  
  185.     /**
  186.      * Tells this component that it is being removed from a container.
  187.      * This is a standard Java AWT method which gets called by the AWT when
  188.      * this component is removed from a container. Typically, it is used to
  189.      * destroy the peers of this component and all its subcomponents.
  190.      *
  191.      * It has been overridden here to stop the nervous text thread.
  192.      *
  193.      * @see #addNotify
  194.      */
  195.     public synchronized void removeNotify() {
  196.         if (jitter != null)
  197.         {
  198.             jitter.stop();
  199.             jitter = null;
  200.         }
  201.  
  202.         super.removeNotify();
  203.     }
  204.  
  205.     /**
  206.      * Makes this component visible.
  207.      * This is a standard Java AWT method which gets called to show this
  208.      * component. If this component was invisible due to a previous hide()
  209.      * call it make this component visible again.
  210.      *
  211.      * @see #hide
  212.      */
  213.     public synchronized void show() {
  214.         super.show();
  215.         if (isVisible()) {
  216.             if (jitter != null)
  217.                 jitter.resume();
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Makes this component invisible.
  223.      * This is a standard Java AWT method which gets called to hide
  224.      * this component. A hidden component cannot be seen by the user nor
  225.      * does it take up space in its container, but it does continue to
  226.      * exist.
  227.      *
  228.      * @see #show
  229.      */
  230.     public synchronized void hide() {
  231.         super.hide();
  232.         if (!isVisible()) {
  233.             if (jitter != null)
  234.                 jitter.suspend();
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * Paints this component using the given graphics context.
  240.      * This is a standard Java AWT method which typically gets called
  241.      * by the AWT to handle painting this component. It paints this component
  242.      * using the given graphics context. The graphics context clipping region
  243.      * is set to the bounding rectangle of this component and its <0,0>
  244.      * coordinate is this component's top-left corner.
  245.      *
  246.      * @param g the graphics context used for painting
  247.      * @see java.awt.Component#repaint
  248.      * @see java.awt.Component#update
  249.      */
  250.     public void paint(java.awt.Graphics g)
  251.     {
  252.         for(int i = 0;i < text.length(); i++)
  253.         {
  254.             int x_coord = (int) (Math.random() * 10 + 20 * i);
  255.             int y_coord = (int) (Math.random() * 10 + 36);
  256.             g.drawChars(separated, i, 1, x_coord, y_coord);
  257.         }
  258.     }
  259.  
  260.  
  261.     /**
  262.      * Returns the recommended dimensions to properly display this component.
  263.      * This is a standard Java AWT method which gets called to determine
  264.      * the recommended size of this component.
  265.      *
  266.      */
  267.     public Dimension getPreferredSize()
  268.     {
  269.         Dimension dim = size();
  270.         Dimension min = getMinimumSize();
  271.  
  272.         return new Dimension(Math.max(dim.width, min.width), Math.max(dim.height, min.height));
  273.     }
  274.  
  275.     /**
  276.      * Returns the minimum dimensions to properly display this component.
  277.      * This is a standard Java AWT method which gets called to determine
  278.      * the recommended size of this component.
  279.      *
  280.      * @return 10x10 per letter
  281.      *
  282.      */
  283.     public Dimension getMinimumSize()
  284.     {
  285.         return new Dimension(separated.length * 10, separated.length * 10);
  286.     }
  287.  
  288.     /**
  289.      * Adds a listener for all event changes.
  290.      * @param PropertyChangeListener listener the listener to add.
  291.      * @see #removePropertyChangeListener
  292.      */
  293.     public void addPropertyChangeListener(PropertyChangeListener listener)
  294.     {
  295.         //super.addPropertyChangeListener(listener);
  296.         changes.addPropertyChangeListener(listener);
  297.     }
  298.  
  299.     /**
  300.      * Removes a listener for all event changes.
  301.      * @param PropertyChangeListener listener the listener to remove.
  302.      * @see #addPropertyChangeListener
  303.      */
  304.     public void removePropertyChangeListener(PropertyChangeListener listener)
  305.     {
  306.         //super.removePropertyChangeListener(listener);
  307.         changes.removePropertyChangeListener(listener);
  308.     }
  309.  
  310.     /**
  311.      * Adds a vetoable listener for all event changes.
  312.      * @param VetoableChangeListener listener the listener to add.
  313.      * @see #removeVetoableChangeListener
  314.      */
  315.     public void addVetoableChangeListener(VetoableChangeListener listener)
  316.     {
  317.          //super.addVetoableChangeListener(listener);
  318.         vetos.addVetoableChangeListener(listener);
  319.     }
  320.  
  321.     /**
  322.      * Removes a vetoable listener for all event changes.
  323.      * @param VetoableChangeListener listener the listener to remove.
  324.      * @see #addVetoableChangeListener
  325.      */
  326.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  327.     {
  328.         //super.removeVetoableChangeListener(listener);
  329.         vetos.removeVetoableChangeListener(listener);
  330.     }
  331.  
  332.     // Private members
  333.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  334.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  335. }
  336.